using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using OfficeOpenXml;
using Serilog;
using UserManagement.Data;

var builder = WebApplication.CreateBuilder(args);
// 数据库配置
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseMySql(builder.Configuration.GetConnectionString("DefaultConnection"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DefaultConnection"))));

// 日志
builder.Host.UseSerilog((ctx, lc) => lc
    .WriteTo.Console()
    .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day));

// 1. 添加基础服务
builder.Services.Scan(scan => 
        scan.FromAssemblyOf<Program>() // 从指定程序集扫描
            .AddClasses()             // 添加所有类
            .AsImplementedInterfaces()    // 自动匹配接口（如 UserService → IUserService）
            .WithScopedLifetime()     // 生命周期
);
// builder.Services.Scan(scan => 
// scan.FromAssemblies(AppDomain.CurrentDomain.GetAssemblies()) // 扫描所有程序集
//     .AddClasses(c => c.Where(t => t.Name.EndsWith("Service"))) // 只注册服务类
//     .AsImplementedInterfaces()   // 注册所有实现的接口
//     .WithSingletonLifetime()
// );
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

// 2. 配置 Swagger
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo 
    { 
        Title = "My API", 
        Version = "v1",
        Contact = new OpenApiContact { Name = "Support", Email = "support@example.com" }
    });
});

// 7.x版本 excel导出授权
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;


var app = builder.Build();

// 3. 中间件管道
app.UseRouting();
app.UseSwagger();
app.UseSwaggerUI(c => 
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    c.RoutePrefix = "swagger"; // 自定义路径
});
app.MapControllers();

app.Run();

